<template>
  <div>
    <head-text titleText="离职交接"></head-text>
    <div class="form_container">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="交接人" prop="receiver">
          <el-select
            v-model="form.receiver"
            placeholder="请选择"
            style="width:600px"
            @change="changeSysuser"
          >
            <el-option
              v-for="data in sysuserSelect"
              :key="data.userId"
              :disabled="userId === data.userId"
              :label="data.userName"
              :value="data.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="发起时间" prop="startDate">
          <el-date-picker
            v-model="form.startDate"
            type="date"
            style="width:600px"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="备注">
          <el-input
            type="textarea"
            v-model="form.remake"
            style="width:600px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import headText from "@c/headText";
import { getSysuserSelectApi, getProcessURLApi } from "@s/api";
import { validateTemplate } from "@u/validate";
export default {
  components: {
    headText
  },
  data() {
    return {
      rules: {
        receiver: [
          { required: true, trigger: "blur", message: "请选择交接人" },
          { validator: validateTemplate },
        ],
        startDate: [
          { required: true, trigger: "blur", message: "请输入发起时间" },
          { validator: validateTemplate },
        ],
      },
      form: {
        receiver: "",
        startDate: "",
        remake: "",
        name: "离职申请",
        receiverName: ""
      },
      userId: "",
      sysuserSelect: [],
    };
  },
  created() {
      let userInfo = JSON.parse(localStorage.getItem("mb_userInfo")) || "{}";
      this.userId = userInfo.userId
  },
  mounted() {
    this.getSysuserSelect("代理人");
  },
  methods: {
    onSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.getProcessAction(this.form);
        }
      });
    },
    // 获取办理人
    getSysuserSelect(val) {
      getSysuserSelectApi({ name: val }).then((res) => {
        this.sysuserSelect = res;
      });
    },
    // 选择办理人
    changeSysuser(val) {
      this.sysuserSelect.map(item => {
        if (val === item.userId) {
          this.form.receiverName = item.userName;
        }
      });
    },
    getProcessAction(val) {
      getProcessURLApi({
        processDefinitionKey: "transfer",
        values: val,
      }).then((res) => {
        if (res) {
          this.$message({
            type: "success",
            message: "申请成功！",
          });
          this.$router.push({ path: `/myProcess` });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.form_container {
  padding: 50px;
}
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
